//
//  PickerBootcamp.swift
//  FCSwiftUI
//
//  Created by 哲思 on 2023/3/1.
//

import SwiftUI

struct PickerBootcamp: View {
    var body: some View {
        SampleViewOne()
        SampleViewTwo()
        SampleViewThree()
        SampleViewFour()
    }
}

private struct SampleViewOne: View {
    // PS: enum类型需要被迭代时,必须符合CaseIterable协议!!!
    enum Season: String, CaseIterable {
        case spring = "🌹"
        case summer = "🌞"
        case autumn = "🍒"
        case winter = "☃️"
    }
    
    @State var preferSeason = Season.summer
    
    var body: some View {
        VStack {
            Divider()
            HStack {
                Label("Picker的.pickerStyle为.segment:", systemImage: "1.circle")
                Spacer()
            }.padding()
            Picker("喜欢的季节", selection: $preferSeason) {
                // Season遵循CaseIterable协议后,可使用allCases属性去迭代
                ForEach(Season.allCases, id: \.self) {season in
                    Text(season.rawValue).tag(season)
                }
            }
            // 这里是水平展示所有可选项的.segment,
            .pickerStyle(.segmented)
            .padding()
            
            Divider()
            HStack {
                Label("Picker的.pickerStyle为.menu:", systemImage: "2.circle")
                Spacer()
            }.padding()
            Picker("喜欢的季节", selection: $preferSeason) {
                // Season遵循CaseIterable协议后,可使用allCases属性去迭代
                ForEach(Season.allCases, id: \.self) {season in
                    Text(season.rawValue).tag(season)
                }
            }
            // 还有只单一显示被选中项目的.menu,其他备选项不显示
            .pickerStyle(.menu)
            .padding()
            
            Divider()
            HStack {
                Label("Picker的.pickerStyle为.inline/.wheel:", systemImage: "3.circle")
                Spacer()
            }.padding()
            Picker("喜欢的季节", selection: $preferSeason) {
                // Season遵循CaseIterable协议后,可使用allCases属性去迭代
                ForEach(Season.allCases, id: \.self) {season in
                    Text(season.rawValue).tag(season)
                }
            }
            // 竖式显示所有可选项的.inline和.wheel
            .pickerStyle(.inline)
            
            Divider()
        }
    }
}

private struct SampleViewTwo: View {
    @State var selection: Int = 0
    
    var body: some View {
        VStack {
            Text("Your Age: \(selection)")
                .font(.largeTitle)

            Picker(selection: $selection) {
                ForEach(18..<100) {
                    Text(String($0)).tag($0)
                        .font(.headline)
                        .foregroundColor(.red)
                }
            } label: {
                Text("Picker")
            }
            .pickerStyle(.wheel)
            .background(Color.brown.opacity(0.1).cornerRadius(10))
            .padding()
        }
    }
}

private struct SampleViewThree: View {
    @State var selection: String = "Most Recent"
    
    let filterOptions: [String] = [
        "Most Recent", "Most Popular", "Most Liked"
    ]

    var body: some View {
        Picker(selection: $selection) {
            ForEach(filterOptions, id: \.self) { option in
                HStack {
                    Text(option).tag(option)
                    Image(systemName: "heart.fill")
                        .foregroundColor(.red)//无效
                }
            }
        } label: {
            // ???: 不知道为什么，这里面的设置都无效
            Text("Picker")
                .font(.headline)//无效
                .foregroundColor(.white)//无效
                .padding()//无效
                .padding(.horizontal)
                .background(Color.red)//无效
                .cornerRadius(10)//无效
                .shadow(color: Color.blue.opacity(0.3), radius: 10, x: 0, y: 10)//无效
        }
        .font(.headline)//无效
        .foregroundColor(.white)//无效
        .tint(.white)
        .padding()
        .padding(.horizontal)
        .background(Color.blue)
        .cornerRadius(10)
        .shadow(color: Color.blue.opacity(0.3), radius: 10, x: 0, y: 10)
    }
}

private struct SampleViewFour: View {
    @State var selection: String = "Most Recent"
    
    let filterOptions: [String] = [
        "Most Recent", "Most Popular", "Most Liked"
    ]
    
    init() {
        UISegmentedControl.appearance().selectedSegmentTintColor = UIColor.red
        
        let attributes: [NSAttributedString.Key : Any] = [.foregroundColor : UIColor.white]
        UISegmentedControl.appearance().setTitleTextAttributes(attributes, for: .selected)
    }
    
    var body: some View {
        Picker(selection: $selection) {
            ForEach(filterOptions.indices, id: \.self) { index in
                Text(filterOptions[index]).tag(filterOptions[index])
            }
        } label: {
            Text("Picker")
        }
        .pickerStyle(.segmented)
        .padding()
    }
}

struct PickerBootcamp_Previews: PreviewProvider {
    static var previews: some View {
        PickerBootcamp()
    }
}
